
package kr.co.casually;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import kr.co.casually.adapter.KeyWordsDbAdapter;
import kr.co.casually.notification.ConcertInfoService;
import kr.co.casually.util.Utils;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class KeyWordRegActivity extends Activity {

	private KeyWordsDbAdapter dbAdapter;

	private EditText etKeyWordTitle;
	private TextView tvStartDate;
	private TextView tvEndDate;
	private ImageView btnKeyWordReg;

	private int genrePosition = 0;

	static final int START_DATE_DIALOG_ID = 1;
	static final int END_DATE_DIALOG_ID = 2;
	private int mYear = 1;
	private int mMonth = 1;
	private int mDay = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.keyword_reg_act);

		etKeyWordTitle = (EditText) findViewById(R.id.keyword_reg_title_et);
		tvStartDate = (TextView) findViewById(R.id.start_date);
		tvEndDate = (TextView) findViewById(R.id.end_date);
		btnKeyWordReg = (ImageView) findViewById(R.id.keword_reg_ok_btn);

		tvStartDate.setOnClickListener(mClickListener);
		tvEndDate.setOnClickListener(mClickListener);
		btnKeyWordReg.setOnClickListener(mClickListener);
		Spinner genreSpinner = (Spinner) findViewById(R.id.genre_spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.genre_name, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		genreSpinner.setAdapter(adapter);
		genreSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				genrePosition = position;
			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

	}

	OnClickListener mClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Calendar c = Calendar.getInstance();
			switch (v.getId()) {

			case R.id.start_date:
				mYear = c.get(Calendar.YEAR);
				mMonth = c.get(Calendar.MONTH);
				mDay = c.get(Calendar.DAY_OF_MONTH);
				showDialog(START_DATE_DIALOG_ID);
				break;

			case R.id.end_date:

				mYear = c.get(Calendar.YEAR);
				mMonth = c.get(Calendar.MONTH);
				mDay = c.get(Calendar.DAY_OF_MONTH);
				showDialog(END_DATE_DIALOG_ID);
				break;

			case R.id.keword_reg_ok_btn:
				if (checkInputData()) {
					insertKeyWord();
				}
				break;
			default:
				break;
			}

		}
	};

	// 시작일자, 종료일자, 명칭 입력값 체크
	private boolean checkInputData() {
		boolean chkFlag = true;
		if (tvStartDate.getText().toString().equals("")
				&& tvEndDate.getText().toString().equals("")
				&& etKeyWordTitle.getText().toString().equals("")) {
			Toast.makeText(this, getString(R.string.keyword_input_check), 1000)
					.show();
			return false;
		}

		return chkFlag;
	}

	// 키워드 등록
	private void insertKeyWord() {

		dbAdapter = new KeyWordsDbAdapter(this);
		dbAdapter.open();
		dbAdapter.createKeyWord(Utils.getGenreCode(this, genrePosition), Utils
				.getGenreName(this, genrePosition), tvStartDate.getText()
				.toString(), tvEndDate.getText().toString(), etKeyWordTitle
				.getText().toString().trim());
		Toast.makeText(this, getString(R.string.keword_reg_success), 1000)
				.show();

		tvStartDate.setText("");
		tvEndDate.setText("");
		etKeyWordTitle.setText("");

		dbAdapter.close();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		startService();
	}

	private String getFromDate(Calendar cal) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
				Locale.KOREA);

		Date date = cal.getTime();
		return formatter.format(date);
	}

	public Calendar getCalendar(String strDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	private void startdDateUpdateDisplayAge() {
		tvStartDate.setText(getFromDate(getCalendar(String.valueOf(mYear + "-"
				+ mMonth + "-" + mDay))));
	}

	private void endDateUpdateDisplayAge() {
		tvEndDate.setText(getFromDate(getCalendar(String.valueOf(mYear + "-"
				+ mMonth + "-" + mDay))));
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case START_DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListenerStart, mYear,
					mMonth, mDay);

		case END_DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListenerEnd, mYear,
					mMonth, mDay);
		}
		return null;

	};

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
		case START_DATE_DIALOG_ID:
			((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
			break;
		case END_DATE_DIALOG_ID:
			((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
			break;
		}
	}

	private DatePickerDialog.OnDateSetListener mDateSetListenerStart = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;
			startdDateUpdateDisplayAge();
		}
	};

	private DatePickerDialog.OnDateSetListener mDateSetListenerEnd = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;
			endDateUpdateDisplayAge();
		}
	};

	private void startService() {
		NotificationManager notificationManager;
		String svcName = Context.NOTIFICATION_SERVICE;
		notificationManager = (NotificationManager) getSystemService(svcName);
		startService(new Intent(this, ConcertInfoService.class));
	}

}
